package com.hz.zbl.service.impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONException;
import com.hz.zbl.client.StudentApi;
import com.hz.zbl.client.TeacherApi;
import com.hz.zbl.entity.BlogEntity;
import com.hz.zbl.entity.CommentsEntity;
import com.hz.zbl.mapper.BlogMapper;
import com.hz.zbl.model.dto.StudentDTO;
import com.hz.zbl.model.dto.TeacherDTO;
import com.hz.zbl.model.enums.ApiResponseCodeEnum;
import com.hz.zbl.model.request.ApiResponse;
import com.hz.zbl.service.BlogService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.swing.*;
import java.util.List;

@Service
public class BlogServiceImpl implements BlogService {
    @Resource
    BlogMapper blogMapper;


    @Resource
    StudentApi studentApi;

    @Resource
    TeacherApi teacherApi;


    @Override
    public int addBlog(String title, String content, int user_id, boolean is_anonymous) {
        BlogEntity blogEntity = new BlogEntity();
        blogEntity.setTitle(title);
        blogEntity.setContent(content);
        blogEntity.setAuthor_id(user_id);
        blogEntity.setPublish_time((int) System.currentTimeMillis());

        if (user_id > 1000){
            try {
                ApiResponse teacher = JSON.parseObject(teacherApi.teacherInfo(user_id), ApiResponse.class);
                TeacherDTO teacherDTO = JSON.parseObject(JSON.toJSONString(teacher.getData()), TeacherDTO.class);

                if(!teacher.getCode().equals(ApiResponseCodeEnum.SUCCESS.getCode())){
                    return 0;
                }
                if (is_anonymous){
                    blogEntity.setIs_anonymous(1);
                    blogEntity.setAnonymous_name("骷髅大王手下的无名小卒" + blogEntity.getAuthor_username());
                }else {
                    blogEntity.setIs_anonymous(0);
                    blogEntity.setAuthor_username(teacherDTO.getUsername());
                    blogEntity.setAuthor_realname(teacherDTO.getRealname());
                }
            } catch (JSONException e) {
                System.out.println("Error parsing JSON: " + e.getMessage());
            }



            System.out.println("teacher blogEntity is : " + blogEntity);
            return blogMapper.addBlog(blogEntity);
        }


        ApiResponse res = JSON.parseObject(studentApi.studentInfo(user_id), ApiResponse.class);


        if (!res.getCode().equals(ApiResponseCodeEnum.SUCCESS.getCode())){
            return 0;
        }

        StudentDTO studentDTO = JSON.parseObject(JSON.toJSONString(res.getData()), StudentDTO.class);

        if (is_anonymous){
            blogEntity.setIs_anonymous(1);
            blogEntity.setAnonymous_name("骷髅大王手下的无名小卒" + blogEntity.getAuthor_username());
        }else {
            blogEntity.setIs_anonymous(0);
            blogEntity.setAuthor_username(studentDTO.getUsername());
            blogEntity.setAuthor_realname(studentDTO.getRealname());
        }


        return blogMapper.addBlog(blogEntity);
    }

    @Override
    public BlogEntity viewBlog(int id) {
        BlogEntity blogEntity = blogMapper.selectBlogById(id);
        if (blogEntity != null) {
            List<CommentsEntity> comments = blogMapper.certainBlogComments(id);
            blogEntity.setComments(comments);
        }
        return blogEntity;
    }

    @Override
    public int deleteBlog(int id) {
        return blogMapper.deleteBlog(id);
    }

    @Override
    public int updateBlog(int id, String title, String content) {

        return blogMapper.updateBlog(id, title, content);
    }

    @Override
    public List<BlogEntity> allBlogs() {
        return blogMapper.allBlogs();
    }

    @Override
    public List<BlogEntity> myBlogs(int author_id) {

        List<BlogEntity> myBlogs = blogMapper.myBlogs(author_id);
        if (myBlogs == null){
            return null;
        }
        return myBlogs;
    }

    @Override
    public int addComments(int blog_id, String username, String content, boolean is_anonymous) {
        CommentsEntity commentsEntity = new CommentsEntity();
        commentsEntity.setBlog_id(blog_id);
        commentsEntity.setContent(content);
        commentsEntity.setPublish_time((int) System.currentTimeMillis());
        commentsEntity.setUsername(is_anonymous ? "不愿透露姓氏的无名小卒" : username);
        commentsEntity.setIs_anonymous(is_anonymous ? 1 : 0);

        return blogMapper.addComment(commentsEntity);
    }

}
